<?php
/**
 * Created by PhpStorm.
 * User: 俊伟
 * Date: 2021/5/21
 * Time: 19:58
 */

namespace app\api\model;

use app\api\model\event\AccountToUser;
use app\api\model\event\Platform;
use app\api\service\token\LoginToken;
use app\lib\exception\account\AccountException;
use LinCmsTp5\admin\model\LinUser;
use think\Db;
use think\Exception;
use think\model\concern\SoftDelete;

class Account extends BaseModel
{
    use SoftDelete;
    protected $table = 'account';
    protected $deleteTime = 'delete_time';
    protected $autoWriteTimestamp = 'datetime';

    /**
     * @param $params
     */
    public static function add($params)
    {
        // 调用当前模型的静态方法create()，第一个参数为要写入的数据，第二个参数标识仅写入数据表定义的字段数据
        static::create($params, true);
    }

    public static function getAccounts(array $conditions){
        $query = new static();
        $size = 200;

        if(!empty($conditions['size'])) $size = $conditions['size'];

        if(!empty($conditions['name'])){
            $query = $query->where('name',"like","%".$conditions['name']."%");
        }

        if(!empty($conditions['app_id'])){
            $query = $query->where('app_id',$conditions['app_id']);
        }

        if(!empty($conditions['is_private'])){
            $user_id = LoginToken::getInstance()->getCurrentUid();
            $query = $query->hasWhere('user',function ($query) use($user_id){
                return $query->where("user_id",$user_id);
            });
        }

        if(!empty($conditions['user_id'])){
            $user_id = $conditions['user_id'];
            $query = $query->hasWhere('user',function ($query) use($user_id){
                return $query->where("user_id",$user_id);
            });
        }

        if(!empty($conditions['distribution'])){
            if($conditions['distribution'] == '未分配'){
                //找出所有绑定过的id
                $query = $query->where('Account.id',"not in",function ($query) {
                    return $query->table('user_account')->field('account_id');
                });
            }
        }

        if(!empty($conditions['platform_id'])){
            $query = $query->where('platform_id',$conditions['platform_id']);
        }

        if (!empty($conditions['remark'])){
            $query = $query->where('remark',"like","%".$conditions['remark']."%");
        }

        if (!empty($conditions['account_category_id'])){
            $query = $query->where('account_category_id',$conditions['account_category_id']);
        }

        if (!empty($conditions['account_type_id'])){
            $query = $query->where('account_type_id',$conditions['account_type_id']);
        }

        return $query->with(['platform','category'])->paginate($size);
    }

    public function platform(){
        return $this->belongsTo(Platform::class,'platform_id','id')->field('id,name');
    }

    public function linUser(){
        RETURN $this->belongsTo(LinUser::class,'uid','id')->field('id,username');
    }
//    public function category(){
//        return $this->belongsTo(DictItem::class,'account_category_id','id');
//    }
//
//    public function user(){
//        return $this->hasMany(UserAccount::class,"account_id",'id');
//    }

    public function accountToUser(){
        return $this->hasMany(AccountToUser::class,"app_id","app_id")->field('app_id,uid');
    }
}
